package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class login_sevlet
 */
@WebServlet("/login_sevlet")
public class login_servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public login_servlet() {
//        super();
        // TODO Auto-generated constructor stub
//        System.out.println("----------login_servlet-----------");
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
//		System.out.println("----------doPost-----------");
		
		
		if("admin".equals(username) && "admin".equals(password))
		{
			//校验通过
			HttpSession session = request.getSession();
			session.setAttribute("username", username);
			
			response.sendRedirect(request.getContextPath()+"/index.jsp");
			
			System.out.println("当前请求的路径1:"+request.getContextPath());
		}
		else
		{
//			System.out.println("当前请求的路径2:"+request.getContextPath());
			
			//校验失败
			response.sendRedirect(request.getContextPath()+"/fail.jsp");
		}
	}

}
